import { getStore, removeStore, setStore } from '@/util/store'
import { getRoles } from "@/api/login";

const user = {
  state: {
    role: getStore({
      name: 'role'
    }) || '',
    access_token: getStore({
      name: 'access_token'
    }) || '',
  },
  actions: {
    getRole({ commit }, role) {
      return new Promise((resolve, reject) => {
        getRoles(role.role).then(res => {
          commit('ADD_ROUTES', res.data)
          resolve(res.data)
        })
      })
    },
    // 登出
    LogOut({ commit }) {
      return new Promise((resolve, reject) => {
        // logout().then(() => {
          commit('ADD_ROUTES', [])
          commit('SET_ACCESS_TOKEN', '')
          commit('SET_ROLE', '')
          console.log(111111111);
          resolve()
        // }).catch(error => {
        //   reject(error)
        // })
      })
    },
  },
  mutations: {
    SET_ACCESS_TOKEN: (state, access_token) => {
      state.access_token = access_token
      setStore({
        name: 'access_token',
        content: state.access_token
      })
    },
    SET_ROLE: (state, role) => {
      state.role = role
      setStore({
        name: 'role',
        content: state.role
      })
    },
  }
}

export default user